﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Collections.Generic;
using Frame.Lib.Core.Log.Login;
using Frame.Lib.Web;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;
using Frame.Lib.Core.User;
using Frame.Lib.Common;


public partial class Admin_Log_LoginList : PageList
{
 

    private LogLoginBO Log
    {
        get
        {
            return LogLoginBO.Instance<LogLoginBO>();
        }
    }

    private string StartTime
    {
        get
        {
            return Request.QueryString["StartTime"];
        }

    }

    private string EndTime
    {
        get
        {
            return Request.QueryString["EndTime"];
        }
    }

    private string PageSize
    {
        get
        {
            return Request.QueryString["PageSize"];
        }
    }

    private string SortDirection
    {
        get
        {
            return (string)ViewState["SortDirection"];
        }
        set
        {
            ViewState["SortDirection"] = value;
        }
    }

    private string SortExpression
    {
        get
        {
            return (string)ViewState["SortExpression"];
        }
        set
        {
            ViewState["SortExpression"] = value;
        }
    }

    private string OrderBy
    {
        get
        {
            return Request.QueryString["OrderBy"];
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            Pager.BindDrp(drpPager);

            if (!string.IsNullOrEmpty(this.PageSize))
            {
                drpPager.SelectedValue = this.PageSize;
            }
            if (!string.IsNullOrEmpty(this.StartTime))
            {
                txtStartTime.Text = this.StartTime;
            }
            if (!string.IsNullOrEmpty(this.EndTime))
            {
                txtEndTime.Text = this.EndTime;
            }

            if (!string.IsNullOrEmpty(this.OrderBy))
            {
                this.SortExpression = this.OrderBy.Split(' ')[0];
                this.SortDirection = this.OrderBy.Split(' ')[1];
            }

            //_setting.SortExpression = "InputTime";
            //_setting.SortDirection = Jzy.Core.Common.SortDirection.desc;
            BindView();
            
            //CustSort.SwapSortDirection(_setting);//必须要在绑定之后
        }
        InitSelectID(hidSelectIDs);
        JSUtil.SetTableHeight("list", 145);
        AddAttr();

        this.IncludeJsFile("calendar", JsFile.Calendar);
    }

    private void AddAttr()
    {
        JSUtil.DelConfirm(btnDelete, "登录日志");
        JSUtil.ClickBtn(lnkDelete, btnDelete);

        WebUtil.SetTimeBox(txtStartTime, false);
        WebUtil.SetTimeBox(txtEndTime, false);

        btnDelete2.Attributes.Add("onclick", "return confirm('确认删除登录日志?')");
        btnClear.Attributes.Add("onclick", "return confirm('确认删除全部登录日志?')");
       

        JSUtil.ClickBtn(lnkClear, btnClear);

       
    }

    protected void sort_Click(object sender, EventArgs e)
    {
        if (this.SortDirection == "desc")
        {
            this.SortDirection = "asc";
        }
        else
        {
            this.SortDirection = "desc";
        }

        // HtmlPager.SetSortImage(sender, this.SortDirection);
        IButtonControl button = sender as IButtonControl;
        this.SortExpression = button.CommandArgument;
        BindView();
    }


    protected string GetResult(object result)
    {
        LoginResult rst = (LoginResult)(int)result;
        switch (rst)
        {
            case LoginResult.Forbidden:
                return "账户被禁用";
            case LoginResult.PwdErr:
                return "密码错";
            case LoginResult.UserErr:
                return "用户名错";
            default:
                return "登录成功";

        }
    }

    private void BindView()
    {
        LVUtil.Init(lv, lvHeader);
        chkSelectAll.Checked = false;

        switch (this.SortExpression)
        {
            case "InputTime":
                HtmlPager.SetSortImage(btnInputTime, this.SortDirection);
                break;
            case "UserName":
                HtmlPager.SetSortImage(btnUserName, this.SortDirection);
                break;
            case "LoginName":
                HtmlPager.SetSortImage(btnLoginName, this.SortDirection);
                break;
            case "UserID":
                HtmlPager.SetSortImage(btnUserID, this.SortDirection);
                break;

        }

        PagerSetting setting = new PagerSetting();
        setting.PageIndex = this.PageIndex;
        setting.PageSize = int.Parse(drpPager.SelectedValue);
        //GVUtil.InitGV(gvList);
        setting.OrderBy = "InputTime desc";
        if (!string.IsNullOrEmpty(this.SortExpression))
        {
            setting.OrderBy = this.SortExpression + " " + this.SortDirection;
        }

        if (txtStartTime.Text != "")
        {
            if (string.IsNullOrEmpty(setting.Where))
            {
                setting.Where += "InputTime >= @p0";
            }
            else
            {
                setting.Where += " and InputTime >= @p0";
            }
        }
        if (txtEndTime.Text != "")
        {
            if (string.IsNullOrEmpty(setting.Where))
            {
                setting.Where += "InputTime <= @p1";
            }
            else
            {
                setting.Where += " and InputTime <= @p1";
            }
        }
        IEnumerable<LogLoginInfo> list = Log.GetList(setting, txtStartTime.Text.Trim(), txtEndTime.Text.Trim());

       
        lv.DataKeyNames = new string[] { "ID" };
        lv.DataSource = list;
        lv.DataBind();


        string prefix = GlobalVar.WebCurrPath + "LoginList.aspx?PageIndex={0}&PageSize=" + drpPager.SelectedValue + "&StartTime=" + txtStartTime.Text + "&EndTime=" + txtEndTime.Text + "&OrderBy=" + setting.OrderBy;


        litPager.Text = HtmlPager.Render(setting, prefix, true);


       

    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (strSelectIDs != "")
        {
            Log.Delete(StrUtil.SplitToStr(strSelectIDs));
            BindView();
        }
    }
    protected void drpPager_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindView();
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        Log.Delete();
        BindView();
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindView();
    }

    protected void btnDelete2_Click(object sender, EventArgs e)
    {
        Log.Delete(txtStartTime.Text, txtEndTime.Text);
        BindView();
    }
}
